package com.hamizhong.iflowers.web.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.IFestivalService;
import com.hamizhong.iflowers.service.IFlowerCountService;
import com.hamizhong.iflowers.service.IFlowerFloridService;
import com.hamizhong.iflowers.service.IGoodsFestivalMapService;
import com.hamizhong.iflowers.service.IGoodsProductMapService;
import com.hamizhong.iflowers.service.IGoodsPurposeMapService;
import com.hamizhong.iflowers.service.IGoodsService;
import com.hamizhong.iflowers.service.IGoodsTargetMapService;
import com.hamizhong.iflowers.service.IProductService;
import com.hamizhong.iflowers.service.IPurposeService;
import com.hamizhong.iflowers.service.ITargetService;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.GoodsVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class AjaxInitIndexGoodsAction extends MyBaseAjaxAction {
	private IGoodsService iGoodsService = null;
	private IProductService iProductService = null;
	private IPurposeService iPurposeService = null;
	private IFestivalService iFestivalService = null;
	private ITargetService iTargetService = null;
	private IFlowerFloridService iFlowerFloridService = null;
	private IFlowerCountService iFlowerCountService = null;
	private IGoodsProductMapService iGoodsProductMapService = null;
	private IGoodsPurposeMapService iGoodsPurposeMapService = null;
	private IGoodsFestivalMapService iGoodsFestivalMapService = null;
	private IGoodsTargetMapService iGoodsTargetMapService = null;
	public IGoodsService getiGoodsService() {
		return iGoodsService;
	}
	public void setiGoodsService(IGoodsService iGoodsService) {
		this.iGoodsService = iGoodsService;
	}
	public IProductService getiProductService() {
		return iProductService;
	}
	public void setiProductService(IProductService iProductService) {
		this.iProductService = iProductService;
	}
	public IPurposeService getiPurposeService() {
		return iPurposeService;
	}
	public void setiPurposeService(IPurposeService iPurposeService) {
		this.iPurposeService = iPurposeService;
	}
	public IFestivalService getiFestivalService() {
		return iFestivalService;
	}
	public void setiFestivalService(IFestivalService iFestivalService) {
		this.iFestivalService = iFestivalService;
	}
	public ITargetService getiTargetService() {
		return iTargetService;
	}
	public void setiTargetService(ITargetService iTargetService) {
		this.iTargetService = iTargetService;
	}
	public IFlowerFloridService getiFlowerFloridService() {
		return iFlowerFloridService;
	}
	public void setiFlowerFloridService(IFlowerFloridService iFlowerFloridService) {
		this.iFlowerFloridService = iFlowerFloridService;
	}
	public IFlowerCountService getiFlowerCountService() {
		return iFlowerCountService;
	}
	public void setiFlowerCountService(IFlowerCountService iFlowerCountService) {
		this.iFlowerCountService = iFlowerCountService;
	}
	public IGoodsProductMapService getiGoodsProductMapService() {
		return iGoodsProductMapService;
	}
	public void setiGoodsProductMapService(
			IGoodsProductMapService iGoodsProductMapService) {
		this.iGoodsProductMapService = iGoodsProductMapService;
	}
	public IGoodsPurposeMapService getiGoodsPurposeMapService() {
		return iGoodsPurposeMapService;
	}
	public void setiGoodsPurposeMapService(
			IGoodsPurposeMapService iGoodsPurposeMapService) {
		this.iGoodsPurposeMapService = iGoodsPurposeMapService;
	}
	public IGoodsFestivalMapService getiGoodsFestivalMapService() {
		return iGoodsFestivalMapService;
	}
	public void setiGoodsFestivalMapService(
			IGoodsFestivalMapService iGoodsFestivalMapService) {
		this.iGoodsFestivalMapService = iGoodsFestivalMapService;
	}
	public IGoodsTargetMapService getiGoodsTargetMapService() {
		return iGoodsTargetMapService;
	}
	public void setiGoodsTargetMapService(
			IGoodsTargetMapService iGoodsTargetMapService) {
		this.iGoodsTargetMapService = iGoodsTargetMapService;
	}
	
	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxInitIndexGoodsAction...");
		JSONObject jo = new JSONObject();
		String onSaleCnt = getRQ().getParameter("onSaleCnt");
		String commonCnt = getRQ().getParameter("commonCnt");
		IOPResultDto<IObjectVO> resultDto = iGoodsService.findIndexGoods(onSaleCnt,commonCnt);
		if(resultDto.isSuccess()) {
			List<GoodsVO> goodsVOOnSaleList = resultDto.getFirstResult().getGoodsVOList();
			List<GoodsVO> goodsVOCommonList = resultDto.getResultList().get(1).getGoodsVOList();
			JSONArray ja = new JSONArray();
			JSONArray ja2 = new JSONArray();
			JSONObject jobj = null;
			for (GoodsVO goodsVO : goodsVOOnSaleList) {
				jobj = new JSONObject();
				jobj.put("goodsId", goodsVO.getGoodsId());
				jobj.put("goodsPictureS", goodsVO.getGoodsPictureS());
				jobj.put("goodsName", goodsVO.getGoodsName());
				jobj.put("goodsPrice", MyTools.formatFloat1PointStr(goodsVO.getGoodsPrice()));
				jobj.put("isBargin", goodsVO.getIsBargin());
				if(goodsVO.getBarginPrice() != null) {
					jobj.put("barginPrice", MyTools.formatFloat1PointStr(goodsVO.getBarginPrice()));
				}else {
					jobj.put("barginPrice", goodsVO.getBarginPrice());
				}
				//jobj.put("flowerFloridName", goodsVO.getFlowerFloridVO().getFlowerFloridName());
				//jobj.put("flowerCountName", goodsVO.getFlowerCountVO().getFlowerCountName());
				jobj.put("sortId", goodsVO.getSortId());
				jobj.put("remarks", goodsVO.getRemarks());
				ja.add(jobj);
			}
			for (GoodsVO goodsVO : goodsVOCommonList) {
				jobj = new JSONObject();
				jobj.put("goodsId", goodsVO.getGoodsId());
				jobj.put("goodsPictureS", goodsVO.getGoodsPictureS());
				jobj.put("goodsName", goodsVO.getGoodsName());
				jobj.put("goodsPrice", MyTools.formatFloat1PointStr(goodsVO.getGoodsPrice()));
				jobj.put("isBargin", goodsVO.getIsBargin());
				if(goodsVO.getBarginPrice() != null) {
					jobj.put("barginPrice", MyTools.formatFloat1PointStr(goodsVO.getBarginPrice()));
				}else {
					jobj.put("barginPrice", goodsVO.getBarginPrice());
				}
				//jobj.put("flowerFloridName", goodsVO.getFlowerFloridVO().getFlowerFloridName());
				//jobj.put("flowerCountName", goodsVO.getFlowerCountVO().getFlowerCountName());
				jobj.put("sortId", goodsVO.getSortId());
				jobj.put("remarks", goodsVO.getRemarks());
				ja2.add(jobj);
			}
			jo.put("data", ja.toString());
			jo.put("data2", ja2.toString());
			jo.put("result", MyConstants.AJAX_SUCCESS);
		}else {
			jo.put("result", MyConstants.AJAX_FAILED_ALERT);
			jo.put("msg", resultDto.getErrorMSG());
		}
		return jo;
	}

}
